//
//  UIViewController+TitleOrImage.m
//  test1
//
//  Created by Hongshi on 15/9/23.
//  Copyright © 2015年 Hongshi. All rights reserved.
//

#import "UIViewController+TitleOrImage.h"

@implementation UIViewController (TitleOrImage)

//更改导航栏上的返回按钮的名称
- (void) createBackButtonWithTitle:(NSString *)title
{
    [self createBackButtonWithTitle:title WithBackgroundImage:nil WithSelectImage:nil];
}

- (void) createBackButtonWithTitle:(NSString *)title WithBackgroundImage:(NSString *) setImage WithSelectImage:(NSString *) selectImage
{
    [self createLeftButtonWithTitle:title WithSetImage:setImage WithSelectImage:selectImage];
}

//使用图片更改导航栏上的返回按钮的名称
- (void)createBackButtonWithTitle:(NSString *)title AndImage:(NSString *)image
{
    [self CreateBarBacButtonWithTitle:title AndImage:image AndRightBarButtonTitle:nil];
}

//更改导航栏上的返回按钮的名称并创建导航栏右边的按钮
- (void)createBackButtonWithTitle:(NSString *)title AndRightButtonTitle:(NSString *)RTitle
{
    [self CreateBarBacButtonWithTitle:title AndImage:nil AndRightBarButtonTitle:RTitle];
}

//使用图片更改导航栏上的返回按钮的名称并创建导航栏右边的按钮
- (void)createBackButtonWithTitle:(NSString *)title AndImage:(NSString *)image AndRightButtonTitle:(NSString *)RTitle
{
    [self CreateBarBacButtonWithTitle:title AndImage:image AndRightBarButtonTitle:RTitle];
}



//创建自定义导航栏返回按钮
-(void) CreateBarBacButtonWithTitle:(NSString *) title AndImage:(NSString *) image AndRightBarButtonTitle:(NSString *) Rtitle
{
    UIView *BarView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 100, 40)];
    BarView.userInteractionEnabled = YES;
  
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 11, 18, 18)];
    imageView.image = [UIImage imageNamed:image];
    imageView.userInteractionEnabled = NO;
    UILabel *label = [[UILabel alloc] init];
    
    if (![image isEqualToString:@""])
    {
        label.frame = CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+5, 0, 40, 40);
    }
    else
    {
        label.frame = CGRectMake(0, 0, 40, 40);
    }
    
    label.text = title;
    label.font = [UIFont boldSystemFontOfSize:16];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.userInteractionEnabled = NO;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [BarView addGestureRecognizer:tap];
    
    if (![image isEqualToString:@""])
    {
        [BarView addSubview:imageView];
    }
    
    [BarView addSubview:label];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:BarView];

    self.navigationItem.leftBarButtonItem = back;

    if (![Rtitle isEqualToString:@""])
    {
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:Rtitle style:UIBarButtonItemStylePlain target:self action:@selector(pressTheButtonAction:)];
        
        [rightBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:16],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItem = rightBtn;
    }
}

- (void) createLeftButtonWithTitle:(NSString *) title WithSetImage:(NSString *) setImage WithSelectImage:(NSString *) selectImage
{
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(-20, 0, 60, 44)];
   
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:title attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:17],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
        
    [leftBtn setAttributedTitle:string forState: UIControlStateNormal];

    [leftBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];//设置按钮点击事件
    [leftBtn setBackgroundImage:[UIImage imageNamed:setImage] forState:UIControlStateNormal];//设置按钮正常状态图片
    [leftBtn setBackgroundImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];//设置按钮选中图片
    UIBarButtonItem *leftBarButon = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0))
        
    {
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        
        negativeSpacer.width = -20;//这个数值可以根据情况自由变化
        self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftBarButon];
    }
    else
    {
        self.navigationItem.leftBarButtonItem = leftBarButon;
    }
}

#pragma mark - tapAction
-(void)tapAction:(UITapGestureRecognizer *) tap
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - pressTheButtonAction
- (void) pressTheButtonAction:(UIButton *) btn
{
    
}
- (void)action:(UIBarButtonItem *) bat
{
    NSLog(@"后退");
}

- (void) btnClicked:(UIButton *) btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
    //[self.navigationController popViewControllerAnimated:YES];
}

@end
